﻿using SimStatus.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SimStatus.UI
{
    /// <summary>
    /// Install_Checking.xaml 的交互逻辑
    /// </summary>
    public partial class Install_Checking : UserControl, IContent
    {
        public Install_Checking()
        {
            InitializeComponent();
            this.Loaded += Install_Checking_Loaded;
        }

        public IMainPage main { get; set; }

        void Install_Checking_Loaded(object sender, RoutedEventArgs e)
        {
            blk_Connections.Visibility = Visibility.Collapsed;
            blk_OpenPort.Visibility = Visibility.Collapsed;
            blk_SIM.Visibility = Visibility.Collapsed;
            Next.Visibility = Visibility.Collapsed;
        }

       public void Do()
        {
            
            //尝试连接端口
            var comstatus = main.ConnectToPort(main.PortName,main.PortNumber);
            var dialers = main.GetAllUserPhoneBook();
            if (comstatus == ConnectResult.Error)
            {
                main.ShowContent(new Errors.Status() { Msg = "无法连接到设备", 
                    Content = "此问题可能由以下问题引发：\n1.未正确安装驱动程序；\n2.端口正被其他进程占用\n\n解决方法：1.尝试安装驱动程序后点击下方的【重试】按钮；\n2.如果您正在使用拨号上网，或使用COM端口调试工具，请断开连接、关闭调试程序或重新启动后再试一次。" });
                Jpp.DoEvent();
            }
            else if (comstatus == ConnectResult.NoSim)
            {
                blk_OpenPort.Visibility = Visibility.Visible;
                Jpp.DoEvent();
                main.ShowContent(new Errors.Status() { Msg = "未检测到SIM卡", Content = "未在您的平板电脑中检测到可用的SIM卡，请以正确的方式在SIM卡插槽中插入SIM卡；如您已经插入SIM卡，并仍然发生此错误，请尝试取出SIM卡并擦拭接触面后重新插入SIM卡插槽，并点击【重试】按钮。" });
                Jpp.DoEvent();
            }
            else if (dialers.Count > 0)
            {
                blk_OpenPort.Visibility = Visibility.Visible;
                blk_SIM.Visibility = Visibility.Visible;
                Jpp.DoEvent();
                StringBuilder str = new StringBuilder();
                dialers.ForEach(p => str.AppendLine(p));
                main.ShowContent(new Errors.Status()
                {
                     Msg="检测到已有的宽带连接",
                     Content = "请删除以下宽带连接后点击【重试】按钮：\n" + str.ToString()
                });
                Jpp.DoEvent();
            }
            else
            {
                blk_OpenPort.Visibility = Visibility.Visible;
                Jpp.DoEvent();
                Thread.Sleep(1000);
                blk_SIM.Visibility = Visibility.Visible;
                Jpp.DoEvent();
                Thread.Sleep(1000);
                 blk_Connections.Visibility = Visibility.Visible;
                 Jpp.DoEvent();
                 Thread.Sleep(1000);
                 Next.Visibility = Visibility.Visible;
            }
            //检测宽带连接            
        }

        void Retry()
        {
            var content = new Install_Checking();
            main.ShowContent(content );
            content.Do();
        }

        private void Next_Click(object sender, RoutedEventArgs e)
        {
            Install_Creating content = new Install_Creating();
            content.main = this.main;
            main.ShowContent(content);
            Jpp.DoEvent();
            content.Do();
        }
    }
}
